//
//  XZContentView.swift
//  高仿斗鱼直播
//
//  Created by 珍 on 17/4/21.
//  Copyright © 2017年 zhenzhen. All rights reserved.
//

import UIKit

private let kContentCellID = "kContentCellID"

class XZContentView: UIView {
    

    fileprivate var ChildsVc: [UIViewController]
    fileprivate var parentvc : UIViewController
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = self.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kContentCellID)
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.scrollsToTop = false
        collectionView.showsHorizontalScrollIndicator = false
        
        return collectionView
    }()

    init(frame: CGRect,ChildVcs:[UIViewController],parentVC:UIViewController) {
    
    
        self.ChildsVc = ChildVcs
        self.parentvc = parentVC
        super.init(frame :frame)
        
        setupUI()
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   

}
extension XZContentView{
    fileprivate func setupUI() {
        // 1.将所有子控制器添加到父控制器中
        for childVc in ChildsVc {
            parentvc.addChildViewController(childVc)
        }
        
        // 2.添加UICollection用于展示内容
        addSubview(collectionView)
    }
}


extension XZContentView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ChildsVc.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kContentCellID, for: indexPath)
        
        for subView in cell.contentView.subviews {
            subView.removeFromSuperview()
        }
        
        let childVc = ChildsVc[indexPath.item]
        childVc.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childVc.view)
        
        return cell
    }
}
